﻿log("Running Basic Information Grab Tool  ...  basicInformation.js")

loadScriptAsync("./utilities.js", main)

function main()
{
    log("Task : Basic Information Grab start!")
    var href = location.href;
    if(has("buyertrade.taobao.com"))
        onTrades()
    else if(has("rate.taobao.com"))
        onRate()
    else if (has("custweb.alipay.com"))
        onRegisterDate();
    else if (has("www.baidu.com/s") && has("wd=ip"))
        onIp();
    else if(has("www.baidu.com"))
        onBaiduIndex();
    else if (has("login.taobao.com"))
            ;// just wait for its reloading
    else if (has("www.taobao.com"))
        onTaobaoIndex()
    function has(obj)
    {
        return href.search(obj) != -1;
    }
}

// www.baidu.com
function onBaiduIndex()
{
    log("task : on baidu index");

    tasks.searchIp = function(arg)
    {
        kw.value = "ip";
        su.click();
        waitForLoaded(onIp);
    }
    msg.send("baiduIndexOpened");
}

// www.baidu.com/s?wd=ip
function onIp()
{
    log("task : on IP")
    tasks.goTaobao = function(arg)
    {
        log("New Task : goTaobao");
        location.href = "http://www.taobao.com"
    }
    function checkTitleChanged()
    {
        if (document.title.search("ip") === -1)
        {
            setTimeout(checkTitleChanged, 100);
        }
        else
        {
            msg.send("loadedIp");
        }
    }
    checkTitleChanged();
}


// www.taobao.com
function onTaobaoIndex()
{
    tasks.goTrades = functionWithSizzle(function (arg)
    {
        log("New Task : goTrades");
        var res = Sizzle("a:contains(已买到的宝贝)");
        res[res.length - 1].click();
    });

    doWithSizzle(function()
    {
        log("Current Task : onTaobaoIndex");
    
        var loginButton = Sizzle("a:contains(亲，请登录)");
        if (loginButton.length > 0)
            loginButton[loginButton.length - 1].click();
        msg.send("taobaoIndexOpened", {});
    })
}

// ---- Trades
function onTrades()
{
    log("Current Task : onTrades");
    var nickname = document.querySelector(".login-info-nick").textContent;
    msg.send("accountNickname", { nickname: nickname });

    tasks.goRate = functionWithSizzle(function (arg)
    {
        log("New Task : goRate");
        var buttons = Sizzle("a:contains(评价管理)");
        buttons[buttons.length - 1].click();
    });

    doWithSizzle(function ()
    {
        var buttons = Sizzle("a:contains(评价管理)");
        if (buttons.length === 0)
            return; // just wait, maybe sige turn to login page
        msg.send("tradesOpened", {});
    })
}
// ---- Rate
function onRate()
{
    log("Current Task : onRate");

    doWithJQuery(function ()
    {

        tasks.goRegisterDate = function (arg)
        {
            log("New Task : goRegisterDate");
            location.href = "https://custweb.alipay.com/account/index.htm";
        }
        msg.send("rateOpened", {});
    });
}

function onRegisterDate()
{
    log("Current Task : onRegisterDate");
    doWithSizzle(function ()
    {
        var targets = Sizzle(":contains(淘宝会员名)");
        if (targets.length == 0)
        {
            log("Waitting for login to Alipay");
        }
        else
        {
            var t = targets[targets.length-1];
            window.scrollBy(0, t.getBoundingClientRect().top - 100*Math.random());
            msg.send("registerDateOpened", {});
        }
    });
}